<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends AdminController {

	function __construct() { 
		parent::__construct();
		$this -> load -> model("User_model");		
		$this -> load -> model('Subject_model');
		$this -> sidebar = "Member-User";
	}

	public function Index() {
		$data['type'] = CommonIdentity::Teacher[XPHEnum::Value];
		$this -> load -> view('admin/userList',$data);
	}

	public function Index1() {
		$data['type'] = CommonIdentity::Student[XPHEnum::Value];
		$this -> load -> view('admin/userList1',$data);
	}

	public function Index2() {
		$data['type'] = CommonIdentity::OutsideTeacher[XPHEnum::Value];
		$this -> load -> view('admin/userList2',$data);
	}

	function getDatas($type,$params = array(),$isExport = false) {
		$role = explode(',', getSess()['sub_role_id']);
        array_push($role, getSess()['role_id']);

		$connar = array();
		if (!$isExport) {
			$params = $_POST;
			$connar = $this -> User_model -> pickPages($connar, $params);
		}

		$connar['orderby'] = "created desc";
		$connar['where']=$this->sqlEscape('is_del=?? ',array(CommonWhether::Not [XPHEnum::Value]));
		$connar['items']='user.*';

		if($type  == CommonIdentity::Teacher[XPHEnum::Value]){
			$connar['where'] .=$this->sqlEscape(' and type=?? ',array(CommonIdentity::Teacher [XPHEnum::Value]));

		}else if($type  == CommonIdentity::Student[XPHEnum::Value]){
			$connar['where'] .=$this->sqlEscape(' and type=?? ',array(CommonIdentity::Student [XPHEnum::Value]));

		}else if($type  == CommonIdentity::OutsideTeacher[XPHEnum::Value]){
			$connar['where'] .=$this->sqlEscape(' and type=?? ',array(CommonIdentity::OutsideTeacher [XPHEnum::Value]));
		}

	   if(in_array(WebRole::Teacher[XPHEnum::Value], $role)){ //角色是老师的时候只能看自己分院的老师
			$connar['where'] .=$this->sqlEscape(' and username=?? ',array($_SESSION['session_zjipc_kycg']['username']));
	   	  	$connar['where'] .=$this->sqlEscape(' and usercode=?? ',array($_SESSION['session_zjipc_kycg']['usercode']));		 
	   }

		if (isset($params['param'])) {
			$connar['where'] .= $this->sqlLikeEscape(" and (username like '%??%' ",array($params['param']));
			$connar['where'] .= $this->sqlLikeEscape(" or usercode like '%??%') ",array($params['param']));
		}

		if (!$isExport) {
			$data = $this -> User_model -> get_page_list($connar);
		} else {
			$data["ret"] = $this -> User_model -> get_list_full($connar);
		}
		foreach ($data['ret'] as $k => $v) {
			 if(in_array(WebRole::Teacher[XPHEnum::Value], $role)){//教师
			 	if(getSess()['usercode'] == $v['usercode'] && $type == 1){
			 		$data['ret'][$k]['is_edit'] = 3;//可编辑和不能删除
			 		$arr[] = 1;
			 	}else{
			 		if($type == 2){
			 			$data['ret'][$k]['is_edit'] = 1;//可编辑和不能删除
			 		}else{
			 			$data['ret'][$k]['is_edit'] = 2;//不可编辑不可删除
			 		}
			 		
			 	}
			 }else if(in_array(WebRole::Sscientifictaff[XPHEnum::Value], $role)){//科研处
			 	$data['ret'][$k]['is_edit'] = 0;//可编辑可删除
			 }else if(in_array(WebRole::SchoolLeader[XPHEnum::Value], $role)){//校领导
			 	$data['ret'][$k]['is_edit'] = 2;//不可编辑不可删除
			 }else{
			 	$data['ret'][$k]['is_edit'] = 2;//不可编辑不可删除
			 }
			//$data['ret'][$k]['username']= $v['username']?$v['username'].'('.$v['username'].')':'（未设置）';
			$data['ret'][$k]['username']= $v['username']?$v['username']:'未设置';
			$data['ret'][$k]['usercode']= $v['usercode']?$v['usercode']:'未设置';
			$data['ret'][$k]['birthday']= $v['birthday']?$v['birthday']:'';
			$data['ret'][$k]['sex'] = (new CommonGender())->show($v['sex']);
			$data['ret'][$k]['createdChar']=date('Y-m-d H:i:s',$v['created']);				
		}

		if (!$isExport) {

			echo json_encode($data);
		} else {
			return $data["ret"];
		}
	}


	function edit($id,$type){

		$data['dept']=(new Dept)->getAllOneD();
		$data['subject']=$this->Subject_model->get_list(array());
		if($type  == CommonIdentity::Teacher[XPHEnum::Value]){

			$data['id'] = $id;
			$data['title'] = "编辑教师基本信息";
			$sex_arr = array();
			foreach ((new CommonGender())->getAll() as $k => $v) {
				$sex_arr[$k]['Value'] = $v[XPHEnum::Value];
				$sex_arr[$k]['Caption'] = $v[XPHEnum::Caption];
			}
			$data['sex_arr'] = $sex_arr;
			if($id == 0){
				$data['detail'] = $this -> User_model -> get_single(array("id" => $id,'type'=>CommonIdentity::Teacher[XPHEnum::Value]));

			}else{
				$data['detail'] = $this -> User_model -> get_single(array("id" => $id,'type'=>CommonIdentity::Teacher[XPHEnum::Value]));
				$data['detail']['sex'] = (new CommonGender())->show($data['detail']['sex']);
				$data['detail']['birthday']=$data['detail']['birthday']?$data['detail']['birthday']:'';
			}

			$data['type'] = CommonIdentity::Teacher[XPHEnum::Value];
			$this -> load -> view('admin/userEdit',$data);
		

		}else if($type  == CommonIdentity::Student[XPHEnum::Value]){

			$data['id'] = $id;
			$data['title'] = "编辑学生基本信息";
			$sex_arr = array();
			foreach ((new CommonGender())->getAll() as $k => $v) {
				$sex_arr[$k]['Value'] = $v[XPHEnum::Value];
				$sex_arr[$k]['Caption'] = $v[XPHEnum::Caption];
			}
			$data['sex_arr'] = $sex_arr;
			if($id == 0){
				$data['detail'] = $this -> User_model -> get_single(array("id" => $id,'type'=>CommonIdentity::Student[XPHEnum::Value]));

			}else{
				$data['detail'] = $this -> User_model -> get_single(array("id" => $id,'type'=>CommonIdentity::Student[XPHEnum::Value]));
				$data['detail']['sex'] = (new CommonGender())->show($data['detail']['sex']);
				$data['detail']['birthday']=$data['detail']['birthday']?$data['detail']['birthday']:'';
			}


			$data['type'] = CommonIdentity::Student[XPHEnum::Value];
			$this -> load -> view('admin/userEdit1',$data);
			
		}else if($type  == CommonIdentity::OutsideTeacher[XPHEnum::Value]){

			$data['id'] = $id;
			$data['title'] = "编辑外校老师基本信息";
			$sex_arr = array();
			foreach ((new CommonGender())->getAll() as $k => $v) {
				$sex_arr[$k]['Value'] = $v[XPHEnum::Value];
				$sex_arr[$k]['Caption'] = $v[XPHEnum::Caption];
			}
			$data['sex_arr'] = $sex_arr;
			if($id == 0){
				$data['detail'] = $this -> User_model -> get_single(array("id" => $id,'type'=>CommonIdentity::OutsideTeacher[XPHEnum::Value]));

			}else{
				$data['detail'] = $this -> User_model -> get_single(array("id" => $id,'type'=>CommonIdentity::OutsideTeacher[XPHEnum::Value]));
			}


			$data['type'] = CommonIdentity::OutsideTeacher[XPHEnum::Value];
			$this -> load -> view('admin/userEdit2',$data);
			
		}		

	}


	//公共接口判断科研人员是否存在若不存在则添加基本信息，若存在则不弹窗添加
	function is_author(){

		$identity = $_POST['identity'];$author_usercode = $_POST['author'];
		if(!isset($identity) || $identity=="-1" ){
			exit(retJson('请选择身份',false));
		}

		
		if($identity == 3){
			if(!isset($author_usercode)|| $author_usercode == ''){
			  exit(retJson('请输入姓名',false));
		   }
		   $con['username'] = $author_usercode;
		}else{
			if(!isset($author_usercode)|| $author_usercode == ''){
			   exit(retJson('请输入工号',false));
		   }
		   $con['usercode'] = $author_usercode;
		}
		
		$con['type'] = $identity;
		$con['is_del'] = CommonWhether::Not [XPHEnum::Value];

		$data = $this -> User_model -> get_single($con);

		if($data){
			exit(retJson('存在',$data['id'],true,$data['username']));
		}else{
			exit(retJson('不存在',false,false));
		}
	}
	
	//保存科研人员基本信息
	function save(){
		$param = $_POST;
		$type = $_POST['type'];
		//老师
		if($type == CommonIdentity::Teacher[XPHEnum::Value]){
				if($param['sex'] ==  CommonGender::Man[XPHEnum::Caption]){
					$param['sex'] =  CommonGender::Man[XPHEnum::Value];
				}else if($param['sex'] ==  CommonGender::Women[XPHEnum::Caption]){
					$param['sex'] = CommonGender::Women[XPHEnum::Value];
				}else{
					$param['sex'] = 0;
				}

			$connar['username'] = $param['username'];//姓名
			$connar['personnel_dept'] = $param['personnel_dept'];//人事单位
			$connar['usercode'] = $param['usercode'];//教工号
			$connar['phone'] = $param['phone'];//手机号
			$connar['title'] = $param['title'];//职称
			$connar['dept_name'] = $param['dept_name'];//所属单位			
			$connar['academic_background'] = $param['academic_background'];//学科背景
			$connar['talent_title'] = $param['talent_title'];//人才称号
			$connar['max_xl'] = $param['max_xl'];//最高学历
			$connar['max_xw'] = $param['max_xw'];//最高学位
			$connar['graduate_school'] = $param['graduate_school'];//毕业院校
			$connar['sex'] = $param['sex'];//性别			
			$connar['birthday'] = $param['birthday'];//出生年月
			$connar['research_direction'] = $param['research_direction'];//研究方向
			$connar['type'] = CommonIdentity::Teacher[XPHEnum::Value];//教师或学生类型
			
		//学生
		}else if($type == CommonIdentity::Student[XPHEnum::Value]){

			if($param['sex'] ==  CommonGender::Man[XPHEnum::Caption]){
					$param['sex'] =  CommonGender::Man[XPHEnum::Value];
				}else if($param['sex'] ==  CommonGender::Women[XPHEnum::Caption]){
					$param['sex'] = CommonGender::Women[XPHEnum::Value];
				}else{
					$param['sex'] = 0;
				}

			$connar['username'] = $param['username'];//姓名
			$connar['major'] = $param['major'];//专业
			$connar['grade'] = $param['grade'];//年级
			$connar['class'] = $param['class'];//班级
			$connar['usercode'] = $param['usercode'];//学号
			$connar['phone'] = $param['phone'];//手机号
			$connar['dept_name'] = $param['dept_name'];//所属单位
			$connar['sex'] = $param['sex'];//性别			
			$connar['birthday'] = $param['birthday'];//出生年月
			$connar['research_direction'] = $param['research_direction'];//研究方向
			$connar['type'] = CommonIdentity::Student[XPHEnum::Value];//教师或学生类型


		}else if($type == CommonIdentity::OutsideTeacher[XPHEnum::Value]){ //外校老师

			$connar['username'] = $param['username'];//姓名
			$connar['usercode'] = $param['usercode'];//工号
			$connar['dept_name'] = $param['dept_name'];//所属单位
			$connar['type'] = CommonIdentity::OutsideTeacher[XPHEnum::Value];//教师或学生类型
		}

		//新增
		if(!isset($param['id']) || $param['id'] = 0){

			if($type == 3){
				$where['where']=$this->sqlEscape('username=??  and type=??',array($param['username'],$type));
			}else{
				$where['where']=$this->sqlEscape('usercode=??  and type=??',array($param['usercode'],$type));
			}
			
			$where['where'] .=$this->sqlEscape(' and is_del=?? ',array(CommonWhether::Not [XPHEnum::Value]));
			$names = $this -> User_model -> get_single_full($where);
						
			if($names){
				if($type == 1){
					exit(retJson('该工号已存在相同的，请注意',false));
				}else if($type == 2){
					exit(retJson('该学号已存在相同的，请注意',false));
				}elseif($type == 3){
					exit(retJson('该姓名已存在相同的，请注意',false));
				}
	 		
			}else{
				 $connar['is_del'] = CommonWhether::Not [XPHEnum::Value];
			     $connar['creator'] = getSess()['id'];			
			     $connar['created'] = time();
				 $res = $this -> User_model -> add($connar);
				 exit(retJson("保存成功.", true,$res));
			}
			
		}else{

			if($type == 3){
		    	$where['where']=$this->sqlEscape('username=??  and type=??',array($param['username'],$type));
			}else{
				$where['where']=$this->sqlEscape('usercode=??  and type=??',array($param['usercode'],$type));
			}

			
			$where['where'] .=$this->sqlEscape(' and is_del=?? ',array(CommonWhether::Not [XPHEnum::Value]));
			$where['where'] .=$this->sqlEscape(' and id <>?? ',array($_POST['id']));
			$names = $this -> User_model -> get_single_full($where);
			
			if($names){
	 			if($type == 1){
					exit(retJson('该工号已存在相同的，请注意',false));
				}else if($type == 2){
					exit(retJson('该学号已存在相同的，请注意',false));
				}elseif($type == 3){
					exit(retJson('该姓名已存在相同的，请注意',false));
				}
			}else{
				$connar['id'] = $_POST['id'];
				$connar['updator'] = getSess()['id'];			
				$connar['updated'] = time();
				$res = $this -> User_model -> update($connar);
				exit(retJson("修改成功.", true));
			}
			
		}

		   


	}
	
	//删除

	function delete($id){
		$list = $this -> User_model -> get_single(array("id" => $id));
		if($list){
			$param['id'] = $id;
			$param['is_del'] =CommonWhether::Whether [XPHEnum::Value];//已删除
			$this -> User_model -> update($param);
		}
			exit(retJson("删除成功.", true));
			
	}

	
	
	
}